<?php
/**
 * --------------------------------------------------------------
 * 文章控制器
 * --------------------------------------------------------------
 * Light Year Doc
 * @author yinq<3331653644@qq.com>
 * --------------------------------------------------------------
 */
namespace app\index\controller;

use app\common\controller\HomeBase;
use app\admin\model\Column as ColumnModel;
use app\admin\model\Document as DocumentModel;

class Article extends HomeBase
{
    /**
     * 首页
     */
    public function index()
    {
        $list    = $this->_getDataList(0);
        $hot_arc = $this->_getHotList(10);
        
        $this->assign([
            'title'   => '文章 - ' . config('app.site_setting.web_site_title'),
            'list'    => $list,
            'hot_arc' => $hot_arc
        ]);
        return $this->fetch();
    }

    /**
     * 列表
     */
    public function listing()
    {
		$id         = input('id/d');
		$route_name = input('route_name/s');
        
    	if (($id <= 0 || !is_numeric($id)) && empty($route_name)) $this->error('参数错误');
        $map['status'] = 1;
        if (!empty($id)) {
            $map['id'] = $id;
        }
        if (!empty($route_name)) {
            $map['route_name'] = $route_name;
        }
        $cinfo   = ColumnModel::where($map)->field('id,title')->find();
        $list    = $this->_getDataList($cinfo['id']);
        $hot_arc = $this->_getHotList(10);

        $this->assign([
            'title'   => $cinfo['title'] . ' - ' . config('app.site_setting.web_site_title'),
            'list'    => $list,
            'hot_arc' => $hot_arc
        ]);
        return $this->fetch('index');
    }

    /**
     * 通用分页数据获取
     * @param integer $column_id 栏目ID
     */
    private function _getDataList($column_id)
    {
        $all_columns = cache('all_column_ids');
        if (empty($all_columns)) {
            $all_columns = ColumnModel::where(['status' => 1])->field('id,pid')->select();
            $all_columns = $all_columns ? object_to_array($all_columns) : null;
            // 非开发模式，缓存所有栏目id
            if (config('app.site_setting.develop_mode') == 0) {
                cache('all_column_ids', $all_columns);
            }
        }
        $sub_columns = get_all_subs($all_columns, $column_id);
        $all_ids     = array_column($sub_columns, 'id');
        if ($column_id > 0) $all_ids = array_merge($all_ids, [$column_id]);

        $list = DocumentModel::view('document', 'id,cid,title,cover,description,tags,clicks,create_time')
                ->view("column", 'title as column_title,route_name as column_route_name', 'column.id = document.cid', 'left')
                ->where(['document.status' => 1, 'document.cid' => $all_ids])
                ->order('document.id desc')
                ->paginate(15);
        
        return $list;
    }

    /**
     * 获取热门文章 - 这里只获取所有文章中阅读最高的
     */
    private function _getHotList($limit)
    {
        $list = DocumentModel::where(['status' => 1])
                ->field('id,title,create_time')
                ->order('clicks desc')
                ->limit($limit)
                ->select();

        return $list;
    }

    /**
     * 阅读
     */
    public function read()
    {
		$id = input('id/d');
    	if ($id <= 0 || !is_numeric($id)) $this->error('参数错误');

        $info    = DocumentModel::getOne($id);
        $hot_arc = $this->_getHotList(10);
        
        DocumentModel::where('id', $id)->setInc('clicks');
        
        $this->assign([
            'title'   => $info['title'] . ' - ' . config('app.site_setting.web_site_title'),
            'hot_arc' => $hot_arc,
            'info'    => $info
        ]);
        return $this->fetch();
    }
}